package com.example.sunsg.okhttptest.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.util.RoundRectHelper;

/**
 * Created by sunsg on 2017/4/21.
 */

public class RoundCornerFrameLayout extends FrameLayout{
    /**默认圆角*/
    public static final int DEFAULT_RADIUS = 5;
    /**圆角*/
    private float mRadius;
    private float mWidth;
    private float mHeight;
    private Path mPath;
    private RectF mRectF;
    private Paint mPaint;
    private RoundRectHelper mHelper = null;
    public RoundCornerFrameLayout(Context context) {
        this(context,null);
    }

    public RoundCornerFrameLayout(Context context, AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public RoundCornerFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RoundCornerFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs);
    }

    private void init(Context context,AttributeSet attrs){
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerFrameLayout);
        try {
            mRadius = (int) typedArray.getDimension(R.styleable.RoundCornerFrameLayout_corners, DEFAULT_RADIUS);
        }catch (Exception e){
            mRadius = DEFAULT_RADIUS;
        }
        finally {
            typedArray.recycle();
        }
        mPath = new Path();
        mRectF = new RectF();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mHelper = new RoundRectHelper(context,attrs,this);
    }

    private void generalRoundCorner(){
        mPath.reset();
        mRectF.set(0,0,mWidth,mHeight);
        mPath.addRoundRect(mRectF,mRadius,mRadius, Path.Direction.CW);
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if(mWidth != w && mHeight != h){
            mWidth = w;
            mHeight = h;
            generalRoundCorner();
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
//        int layerId = canvas.saveLayer(mRectF, mPaint, Canvas.ALL_SAVE_FLAG);
        canvas.clipPath(mPath);
        super.dispatchDraw(canvas);
//        canvas.restoreToCount(layerId);
    }

    public int dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
}
